package com.classes.attributes.logic;

import java.nio.ByteBuffer;

public class Attribute_stackmaptable extends AttributeAbstract {

    public Attribute_stackmaptable(short nameIndex, int attributeLength) {
        super(nameIndex, attributeLength);
    }

    @Override
    public void parse(ByteBuffer buffer) {
        // 剩余长度
        int length = getAttributeLength() - 6;

        byte[] bytes = new byte[length];
//		buffer.get(bytes);
        for (int i = 0; i < length; i++) {
            buffer.get();
        }
//		short numOfEntries = buffer.getShort();
//		System.out.println("+++++++++++++++++++++++++++++++++");
//		System.out.println("numOfEntries=" + numOfEntries);
//		for (int i = 0; i < numOfEntries; i++) {
//			same_frame(buffer);
//			same_locals_1_stack_item_frame(buffer);
//			same_locals_1_stack_item_frame_extended(buffer);
//			chop_frame(buffer);
//			same_frame_extended(buffer);
//			append_frame(buffer);
//			full_frame(buffer);
//		}
//		System.out.println("+++++++++++++++++++++++++++++++++");
    }

    private void same_frame(ByteBuffer buffer) {
        byte frame_type = buffer.get();
        System.out.println("same_frame:frame_type=" + frame_type);

    }

    private void same_locals_1_stack_item_frame(ByteBuffer buffer) {
        byte frame_type = buffer.get();
        System.out.println("same_locals_1_stack_item_frame:frame_type=" + frame_type);


        Verification_type_info vti = new Verification_type_info();
        vti.top_variable_info(buffer);
        vti.integer_variable_info(buffer);
        vti.float_variable_info(buffer);
        vti.long_variable_info(buffer);
        vti.double_variable_info(buffer);
        vti.null_variable_info(buffer);
        vti.uninitializedThis_variable_info(buffer);
        vti.object_variable_info(buffer);
        vti.uninitiated_variable_info(buffer);

    }

    private void same_locals_1_stack_item_frame_extended(ByteBuffer buffer) {
        byte frame_type = buffer.get();
        short offset_delta = buffer.getShort();
        System.out.println("frame_type=" + frame_type + " offset_delta" + offset_delta);

        Verification_type_info vti = new Verification_type_info();
        vti.top_variable_info(buffer);
        vti.integer_variable_info(buffer);
        vti.float_variable_info(buffer);
        vti.long_variable_info(buffer);
        vti.double_variable_info(buffer);
        vti.null_variable_info(buffer);
        vti.uninitializedThis_variable_info(buffer);
        vti.object_variable_info(buffer);
        vti.uninitiated_variable_info(buffer);
    }

    private void chop_frame(ByteBuffer buffer) {
        byte frame_type = buffer.get();
        short offset_delta = buffer.getShort();
        System.out.println("frame_type=" + frame_type + " offset_delta" + offset_delta);
    }

    private void same_frame_extended(ByteBuffer buffer) {
        byte frame_type = buffer.get();
        short offset_delta = buffer.getShort();
        System.out.println("frame_type=" + frame_type + " offset_delta" + offset_delta);
    }

    private void append_frame(ByteBuffer buffer) {
        byte frame_type = buffer.get();
        short offset_delta = buffer.getShort();
        System.out.println("frame_type=" + frame_type + " offset_delta" + offset_delta);

        for (int i = 0; i < frame_type - 251; i++) {
            Verification_type_info vti = new Verification_type_info();
            vti.top_variable_info(buffer);
            vti.integer_variable_info(buffer);
            vti.float_variable_info(buffer);
            vti.long_variable_info(buffer);
            vti.double_variable_info(buffer);
            vti.null_variable_info(buffer);
            vti.uninitializedThis_variable_info(buffer);
            vti.object_variable_info(buffer);
            vti.uninitiated_variable_info(buffer);
        }
    }

    private void full_frame(ByteBuffer buffer) {

        byte frame_type = buffer.get();
        short offset_delta = buffer.getShort();
        System.out.println("frame_type=" + frame_type + " offset_delta" + offset_delta);


        short number_of_locals = buffer.getShort();
        for (int i = 0; i < number_of_locals; i++) {
            Verification_type_info vti = new Verification_type_info();
            vti.top_variable_info(buffer);
            vti.integer_variable_info(buffer);
            vti.float_variable_info(buffer);
            vti.long_variable_info(buffer);
            vti.double_variable_info(buffer);
            vti.null_variable_info(buffer);
            vti.uninitializedThis_variable_info(buffer);
            vti.object_variable_info(buffer);
            vti.uninitiated_variable_info(buffer);
        }

        short number_of_stack_items = buffer.getShort();
        for (int i = 0; i < number_of_stack_items; i++) {
            Verification_type_info vti = new Verification_type_info();
            vti.top_variable_info(buffer);
            vti.integer_variable_info(buffer);
            vti.float_variable_info(buffer);
            vti.long_variable_info(buffer);
            vti.double_variable_info(buffer);
            vti.null_variable_info(buffer);
            vti.uninitializedThis_variable_info(buffer);
            vti.object_variable_info(buffer);
            vti.uninitiated_variable_info(buffer);
        }

    }


    class Verification_type_info {
        public void top_variable_info(ByteBuffer buffer) {
            byte tag = buffer.get();
            System.out.println("top_variable_info:tag=" + tag);
        }

        public void integer_variable_info(ByteBuffer buffer) {
            byte tag = buffer.get();
            System.out.println("integer_variable_info: tag=" + tag);
        }

        public void float_variable_info(ByteBuffer buffer) {
            byte tag = buffer.get();
            System.out.println("float_variable_info: tag=" + tag);
        }

        public void long_variable_info(ByteBuffer buffer) {
            byte tag = buffer.get();
            System.out.println("long_variable_info: tag=" + tag);
        }

        public void double_variable_info(ByteBuffer buffer) {
            byte tag = buffer.get();
            System.out.println("double_variable_info: tag=" + tag);
        }

        public void null_variable_info(ByteBuffer buffer) {
            byte tag = buffer.get();
            System.out.println("null_variable_info: tag=" + tag);
        }

        public void uninitializedThis_variable_info(ByteBuffer buffer) {
            byte tag = buffer.get();
            System.out.println("uninitializedThis_variable_info: tag=" + tag);
        }

        public void object_variable_info(ByteBuffer buffer) {
            byte tag = buffer.get();
            short index = buffer.getShort();
            System.out.println("object_variable_info:tag=" + tag + " index=" + index);
        }

        public void uninitiated_variable_info(ByteBuffer buffer) {
            byte tag = buffer.get();
            short offset = buffer.getShort();
            System.out.println("uninitiated_variable_info: tag=" + tag + "  offset=" + offset);
        }
    }
}
